package cn.edu.jsu.zjj.controller;


import cn.edu.jsu.zjj.common.result.PageResult;
import cn.edu.jsu.zjj.common.result.Result;
import cn.edu.jsu.zjj.model.dto.operlog.OperLogQueryRequest;
import cn.edu.jsu.zjj.model.entity.OperLog;
import cn.edu.jsu.zjj.model.entity.User;
import cn.edu.jsu.zjj.service.OperLogService;
import cn.edu.jsu.zjj.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 操作日志记录 前端控制器
 * </p>
 *
 * @author fyt
 * @since 2024-05-12
 */
@RestController
@RequestMapping("/oper-log")
@Api(tags = "日志相关接口")
@Slf4j
public class OperLogController {

    @Autowired
    private OperLogService operLogService;
    @Autowired
    private UserService userService;

    @ApiOperation("日志分页查询")
    @PostMapping("/page")
//    public Result<Page> page(@RequestBody OperLogQueryRequest operLogQueryRequest) {
//        log.info("日志分页查询，参数为：{}", operLogQueryRequest);
//        User user = userService.getLoginUser();
////        operLogQueryRequest.setUserId(user.getId());
////        operLogQueryRequest.setUserName(user.getName());
//        //以上新增userId和userName属性
//        operLogQueryRequest.setOperName(user.getName());
//        long size = operLogQueryRequest.getPageSize();
//        int current = operLogQueryRequest.getCurrent();
//        Page<OperLog> operLogPage = operLogService.page(new Page<>(current, size),
//                operLogService.getQueryWrapper(operLogQueryRequest));
//        Page<OperLogDto> operLogDtoPage = new Page<>();
//        //对象拷贝
//        BeanUtils.copyProperties(operLogPage,operLogDtoPage,"records");
//        List<OperLog> records = operLogPage.getRecords();
//        List<OperLogDto> list = records.stream().map((record) -> {
//            OperLogDto operLogDto = new OperLogDto();
//            BeanUtils.copyProperties(record, operLogDto);
//
//            //新增操作人角色
//            if (record.getUserId() != null) {
//                LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                userLambdaQueryWrapper.eq(User::getId, record.getUserId());
//                User onlineUserRole = userService.getOne(userLambdaQueryWrapper);
//                if (onlineUserRole == null) {
//                    operLogDto.setRole("未知");
//                }
//                operLogDto.setRole(onlineUserRole.getRoles());
//            }
//            return operLogDto;
//        }).collect(Collectors.toList());
//
//        operLogDtoPage.setRecords(list);
//        return Result.success(operLogDtoPage);
//    }

    public Result<PageResult> page(@RequestBody OperLogQueryRequest operLogQueryRequest) {
        log.info("日志分页查询，参数为：{}", operLogQueryRequest);
        User user = userService.getLoginUser();
//        operLogQueryRequest.setUserId(user.getId());
//        operLogQueryRequest.setUserName(user.getName());
        //以上新增userId和userName属性
//        operLogQueryRequest.setOperName(user.getName());
        long size = operLogQueryRequest.getPageSize();
        int current = operLogQueryRequest.getCurrent();
        Page<OperLog> operLogPage = operLogService.page(new Page<>(current, size),
                operLogService.getQueryWrapper(operLogQueryRequest));

        return Result.success(new PageResult(operLogPage.getTotal(), operLogPage.getRecords()));
    }

}
